import torch

dev = torch.device("cpu")
# cuda -> GPU
dev = torch.device("cuda:0")
a = torch.tensor([2, 2], 
                 dtype = torch.float32, 
                 device=dev)
print(a)
# 稀疏张量
i = torch.tensor([[0, 1, 2], [0, 1, 2]]) # 非0元素坐标
v = torch.tensor([1, 2, 3]) # 非0元素具体值
a = torch.sparse_coo_tensor(i, v, (4, 4))
print(a)
a = torch.sparse_coo_tensor(i, v, (4, 4)).to_dense() # 转为稠密张亮
print(a)
a = torch.sparse_coo_tensor(i, v, (4, 4),
                            dtype=torch.float32,
                            device=dev).to_dense() # 转为float
print(a)